/**
 * 
 */
package com.nd.android.payment.ui.param;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.widget.Toast;
import antistatic.spinnerwheel.AbstractWheel;
import antistatic.spinnerwheel.OnWheelScrollListener;

import com.common.StarAppConfig;
import com.common.StarAppConst;
import com.common.allCommon.CommUtil;
import com.nd.android.payment.R;
import com.nd.android.payment.ui.dialog.CommonRechargeDlg;
import com.nd.android.payment.ui.dialog.CommonRechargeDlg.OnCommonRechargeClickListener;
import com.nd.android.payment.ui.task.CommonBuyTask;
import com.nd.android.payment.ui.task.ICommonBuyCallBack;

/**
 * 
 * <br>
 * Created 2014-11-3 下午5:44:28
 * 
 * @version
 * @author zhangqr
 * 
 * @see
 */

public class CommonBuyGold implements ICommonRecharge {
    /** 星钻兑换金币 */
    public static int TYPE_GOLD_RECHARGE = 0;
    /** 消费时，金币不足，星钻足够 */
    public static int TYPE_GOLD_BUY_DIAMOND_ENOUGH = 1;
    /** 消费时，金币不足，星钻不足 */
    public static int TYPE_GOLD_BUY_DIAMOND_NOT_ENOUGH = 2;

    /** 标题 */
    private String mStrTitle;
    
    /**
     * 标题 - 剩余的提示
     */
    private String mStrTitleRest;
    
    /**
     * 标题 - 兑换率的提示
     */
    private String mStrTitleRate;

    /** 滚动框里面默认显示第几个 */
    private int mIntDefaultItemIndex;

    /** 剩余币种数量的提示 */
    private String mStrRestTip;

    /** 兑换率提示 */
    private String mStrRateTip;

    /** 提示 */
    private String mStrTip;

    /** 左按钮文本 */
    private String mStrBtnLeft;

    /** 右按钮文本 */
    private String mStrBtnRight;

    /** 所加载的layout资源ID */
    private int mLayoutResID;
    
    /** 是否显示滚动框 */
    private boolean mIsShowWheelView;

    /** 滚动框显示数据 */
    private ArrayList<Object> mDataList = new ArrayList<Object>();
    
    /** 按钮点击监听 */
    private OnCommonRechargeClickListener mListener;
    
    /** 滚动框滚动监听 */
    private OnWheelScrollListener mWheelScrollListener;
    
    /** 金币和星钻都不足 */
    private final int DIAMOND_NOT_ENOUGH = 1;
    /** 金币不足，星钻足 */
    private final int DIAMOND_ENOUGH = 2;
    /** 类型 */
    private int mType = DIAMOND_ENOUGH;
    
    private int mActionType;
    
    /** 上下文 */
    private Activity mActivity;

    /** 所需金币 */
    private int mIntNeedGold;
    /** 剩余金币 */
    private int mIntRestGold;
    /** 剩余星钻 */
    private int mIntRestDiamond;
    
    /**
     * 充值星钻
     */
    private CommonRechargeDlg mBuyDiamondDlg;
    
    /**
     * 回调
     */
    private ICommonBuyCallBack mCallBack;
    
    /**
     * 当操作结束时是否关闭窗口
     */
    private boolean isDismissWhenDone = true;
    
    /**
     * 兑换率
     */
    private int mIntPayRate;
    
    /**
     * 构造函数 - 星钻兑换金币
     * Creates a new instance of CommonBuyGold.
     * <br>Created 2014-11-14 上午11:41:04
     * @param activity      activity
     * @param restGold      剩余的金币数
     * @param restDiamond   剩余的星钻数
     * @param retCallBack   回调
     */
    public CommonBuyGold(Activity activity,
            int restGold, int restDiamond,
            ICommonBuyCallBack retCallBack) {
        this(activity, StarAppConst.PAY.TYPE_RECHARGE, -1, restGold, restDiamond, null, null, retCallBack);
    }
    
    /**
     * 构造函数 - 消费时，金币兑换
     * Creates a new instance of CommonBuyGold.
     * <br>Created 2014-11-14 上午11:41:26
     * @param activity      activity
     * @param needGold      所需的金币数
     * @param restGold      剩余的金币数
     * @param restDiamond   剩余的星钻数
     * @param retCallBack   回调
     */
    public CommonBuyGold(Activity activity,
            int needGold, int restGold, int restDiamond,
            ICommonBuyCallBack retCallBack) {
        this(activity, StarAppConst.PAY.TYPE_BUY, needGold, restGold, restDiamond, null, null, retCallBack);
    }
    
    /**
     * 实例化 - 兑换金币
     * Creates a new instance of CommonBuyGold.
     * <br>Created 2014-11-14 上午11:41:54
     * @param activity      activity
     * @param actionType        
     *      动作类型。消费/充值.
     *      StarAppConst.PAY.TYPE_RECHARGE / StarAppConst.PAY.TYPE_BUY
     * @param needGold      所需的金币数
     * @param restGold      剩余的金币数
     * @param restDiamond   剩余的星钻数
     * @param clickListener 点击监听
     * @param wheelListener 滚动框监听
     * @param retCallBack   回调
     */
    public CommonBuyGold(Activity activity, int actionType,
            int needGold, int restGold, int restDiamond,
            OnCommonRechargeClickListener clickListener,
            OnWheelScrollListener wheelListener,
            ICommonBuyCallBack retCallBack) {
        this.mActivity = activity;
        
        this.mActionType = actionType;
        
        this.mIntNeedGold = needGold;
        this.mIntRestGold = restGold;
        this.mIntRestDiamond = restDiamond;
        
        this.mListener = clickListener;
        this.mWheelScrollListener = wheelListener;
        this.mCallBack = retCallBack;
        
        initValue();
        
        initClickListener();
        
        initWheelScrollListener();
    }
    
    private void initValue() {
        /** 当操作结束时是否关闭窗口 */
        isDismissWhenDone = true;
        
        mIsShowWheelView = true;
        
        /** 所加载的layout资源ID */
//        mLayoutResID = R.layout.common_pay_buy_gold;
        mLayoutResID = 0;
        
        if(StarAppConst.PAY.TYPE_RECHARGE == mActionType
                && mIntRestDiamond != 0) {//充值
            
            /** 标题 */
            mStrTitle = mActivity.getResources().getString(R.string.pay_buy_gold_title);

            if(mIntRestDiamond >= 100000) {
                mStrTitleRest = String.format(mActivity.getString(R.string.pay_buy_gold_title_center_10w), mIntRestDiamond+"");
            } else {
                mStrTitleRest = String.format(mActivity.getString(R.string.pay_buy_gold_title_center), mIntRestDiamond+"");
            }

            mStrTitleRate = String.format(mActivity.getString(R.string.pay_buy_gold_title_right), getPayRate()+"");
            
            /** 滚动框里面默认显示第几个 */
            mIntDefaultItemIndex = 0;
            
            /** 滚动框显示数据 */
            int avaliable = mIntRestDiamond * getPayRate();
            
            int start = getPayRate();
            if(mIntNeedGold % getPayRate() == 0) {
                start = mIntNeedGold;
            } else {
                int ret = mIntNeedGold / getPayRate();
                start = (ret + 1) * getPayRate();
            }
            
            while(start < StarAppConst.PAY.BUY_GOLD_MAX) {
                if(start > avaliable) {
                    break;
                }
                mDataList.add(start);
                start = start + getPayRate();
            }
            
            mStrBtnLeft = mActivity.getResources().getString(R.string.pay_buy_gold_lbtn);
            mStrBtnRight = mActivity.getResources().getString(R.string.pay_buy_gold_rbtn_diamond_enough);
            
            mType = DIAMOND_ENOUGH;
            
        } else if(StarAppConst.PAY.TYPE_BUY == mActionType
                && mIntRestDiamond != 0) {//消费时
            int avaliable = mIntRestDiamond * getPayRate();
            if(mIntNeedGold <= (mIntRestGold + avaliable)) {
                //金币不足，星钻足够
                
                /** 标题 */
                mStrTitle = mActivity.getResources().getString(R.string.pay_buy_gold_title);
                
                /** 滚动框里面默认显示第几个 */
                mIntDefaultItemIndex = 0;
                
                /** 滚动框显示数据 */
                int start = getPayRate();
                if(mIntNeedGold % getPayRate() == 0) {
                    start = mIntNeedGold;
                } else {
                    int ret = mIntNeedGold / getPayRate();
                    start = (ret + 1) * getPayRate();
                }
                
                while(start < StarAppConst.PAY.BUY_GOLD_MAX) {
                    if(start > avaliable) {
                        break;
                    }
                    mDataList.add(start);
                    start = start + getPayRate();
                }
                
                mStrBtnLeft = mActivity.getResources().getString(R.string.pay_buy_gold_lbtn);

                if(mIntRestDiamond >= 100000) {
                    mStrTitleRest = String.format(mActivity.getString(R.string.pay_buy_gold_title_center_10w), mIntRestDiamond+"");
                } else {
                    mStrTitleRest = String.format(mActivity.getString(R.string.pay_buy_gold_title_center), mIntRestDiamond+"");
                }

                mStrTitleRate = String.format(mActivity.getString(R.string.pay_buy_gold_title_right), getPayRate()+"");
                
                mStrBtnRight = mActivity.getResources().getString(R.string.pay_buy_gold_rbtn_diamond_enough);
                
                mType = DIAMOND_ENOUGH;
                
            } else {//金币不足，星钻不足
                mIsShowWheelView = false;
                
                mStrTitle = "";
                mStrTitleRest = "";
                mStrTitleRate = "";
                
                mStrRestTip = String.format(mActivity.getString(R.string.pay_buy_gold_tip_rest_buy_diamond), mIntRestDiamond+"");
                mStrRateTip = String.format(mActivity.getString(R.string.pay_buy_gold_tip_rate_buy_diamond), getPayRate()+"");
                
                mStrTip = mActivity.getResources().getString(R.string.pay_buy_gold_diamond_not_enough_tip);
                
                mStrBtnRight = mActivity.getResources().getString(R.string.pay_buy_gold_rbtn_diamond_not_enough);
                
                mType = DIAMOND_NOT_ENOUGH;
            }
        } else if(mIntRestDiamond == 0){//星钻=0
            mIsShowWheelView = false;
            
            mStrTitle = "";
            mStrTitleRest = "";
            mStrTitleRate = "";
            
            mStrRestTip = String.format(mActivity.getString(R.string.pay_buy_gold_tip_rest_buy_diamond), mIntRestDiamond+"");
            mStrRateTip = String.format(mActivity.getString(R.string.pay_buy_gold_tip_rate_buy_diamond), getPayRate()+"");
            
            mStrTip = mActivity.getResources().getString(R.string.pay_buy_gold_diamond_not_enough_tip);
            
            mStrBtnRight = mActivity.getResources().getString(R.string.pay_buy_gold_rbtn_diamond_not_enough);
            
            mType = DIAMOND_NOT_ENOUGH;
            
        }
    }
    
    public int getPayRate() {
        int rate = StarAppConst.PAY.DEFAULT_RATE;
        try{
            rate = Integer.parseInt(StarAppConfig.getConfig(StarAppConfig.CONFIG_PAY_RATE));
        } catch(Exception e) {
            rate = StarAppConst.PAY.DEFAULT_RATE;
        }
        return rate;
    }
    
    /** 
     * 按钮点击监听
     * 
     * <br>Created 2014-11-4 下午2:08:27
     * @author       zhangqr
     */
    @SuppressLint("ShowToast")
    private void initClickListener() {
        if(mListener == null) {
            mListener = new OnCommonRechargeClickListener() {
                
                @Override
                public void onRightBtnClicked(int count) {
                    isDismissWhenDone = true;
                    if (!CommUtil.JudgeNetWorkStatus(mActivity)) {//网络不可用
                        String failConnect = mActivity.getString(R.string.pay_failed_connect);
                        if(mCallBack != null) {
                            mCallBack.onFail(failConnect);
                        } else {
                            Toast.makeText(mActivity, failConnect, Toast.LENGTH_SHORT);
                        }
                        return ;
                    }
                    if(StarAppConst.PAY.TYPE_RECHARGE == mActionType
                            && mIntRestDiamond != 0) {//充值
                        isDismissWhenDone = true;
                        
                        new CommonBuyTask(mActivity, CommonBuyTask.FROM_BUY_GOLD, mCallBack).execute(count);
                        
                    } else if(StarAppConst.PAY.TYPE_BUY == mActionType
                            && mIntRestDiamond != 0) {//消费
                        if(DIAMOND_ENOUGH == mType) {//金币不足，星钻足够
                            isDismissWhenDone = true;
                            
                            new CommonBuyTask(mActivity, CommonBuyTask.FROM_BUY_GOLD, mCallBack).execute(count);
                            
                        } else if(DIAMOND_NOT_ENOUGH == mType) {//金币不足，星钻不足
                            isDismissWhenDone = true;
                            
                            int needDiamond = StarAppConst.PAY.BUY_DIAMOND_MIN;
                            int restNeedGold = mIntNeedGold - mIntRestGold - mIntRestDiamond*getPayRate();
                            
                            int ret = restNeedGold / getPayRate();
                            if(restNeedGold % getPayRate() == 0) {
                                needDiamond = ret;
                            } else {
                                needDiamond = ret + 1;
                            }
                            
                            mBuyDiamondDlg = new CommonRechargeDlg(mActivity, R.style.common_dialog_style,
                                    new CommonBuyDiamond(mActivity, needDiamond, mCallBack));
                            
                            mBuyDiamondDlg.show();
                        }
                        
                    } else if(mIntRestDiamond == 0) {
                        isDismissWhenDone = true;
                        
                        mBuyDiamondDlg = new CommonRechargeDlg(mActivity, R.style.common_dialog_style,
                                new CommonBuyDiamond(mActivity, mCallBack));
                        
                        mBuyDiamondDlg.show();
                    }
                    
                }
                
                @Override
                public void onLeftBtnClicked() {
                    Toast.makeText(mActivity, "gold left button", Toast.LENGTH_SHORT);
                    
                }
            };
        }
        
    }
    
    /** 
     * 滚动框滚动监听
     * 
     * <br>Created 2014-11-4 下午2:09:03
     * @author       zhangqr
     */
    private void initWheelScrollListener() {
        if(mWheelScrollListener == null) {
            mWheelScrollListener = new OnWheelScrollListener() {
                /**
                 * 开始
                 * <br>Created 2014-11-1 下午5:06:50
                 * @param wheel
                 * @author       张清荣
                 */
                public void onScrollingStarted(AbstractWheel wheel) {
                    
                };
                
                /**
                 * 结束
                 * <br>Created 2014-11-1 下午5:06:50
                 * @param wheel
                 * @author       张清荣
                 */
                @Override
                public void onScrollingFinished(AbstractWheel wheel) {
//                    int itemIndex = wheel.getCurrentItem();
//                    if(mDataList != null && mDataList.size() > 0) {
//                        int value = getPayRate();
//                        try {
//                            value = Integer.parseInt(mDataList.get(itemIndex).toString());
//                            
//                            int availableMax = mIntRestDiamond*getPayRate();
//                            
//                            if(value > availableMax) {
//                                itemIndex = (availableMax - getPayRate()) / getPayRate();
//                            }
//                        } catch (Exception e) {
//                            itemIndex = 0;
//                        }
//                        wheel.setCurrentItem(itemIndex);
//                    }
                }
                
            };
        }
    }
    
    /**
     * 设置标题
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrTitle
     *            标题
     * @author zhangqr
     */
    public void setmStrTitle(String mStrTitle) {
        this.mStrTitle = mStrTitle;
    }
    
    /**
     * 设置滚动框里面默认显示第几个
     * 
     * <br>
     * Created 2014-11-3 下午8:41:50
     * 
     * @param mIntDefaultItemIndex
     *            滚动框里面默认显示第几个
     * @author zhangqr
     */
    public void setmIntDefaultItemIndex(int mIntDefaultItemIndex) {
        this.mIntDefaultItemIndex = mIntDefaultItemIndex;
    }
    
    /**
     * 设置剩余币种数量的提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrRestTip
     *            剩余币种数量的提示
     * @author zhangqr
     */
    public void setmStrRestTip(String mStrRestTip) {
        this.mStrRestTip = mStrRestTip;
    }
    
    /**
     * 设置兑换率提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrRateTip
     *            兑换率提示
     * @author zhangqr
     */
    public void setmStrRateTip(String mStrRateTip) {
        this.mStrRateTip = mStrRateTip;
    }
    
    /**
     * 设置提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrTip
     *            提示
     * @author zhangqr
     */
    public void setmStrTip(String mStrTip) {
        this.mStrTip = mStrTip;
    }
    
    /**
     * 设置左按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrBtnLeft
     *            左按钮文本
     * @author zhangqr
     */
    public void setmStrBtnLeft(String mStrBtnLeft) {
        this.mStrBtnLeft = mStrBtnLeft;
    }

    /**
     * 设置左按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrBtnRight
     *            左按钮文本
     * @author zhangqr
     */
    public void setmStrBtnRight(String mStrBtnRight) {
        this.mStrBtnRight = mStrBtnRight;
    }

    /**
     * 设置所加载的layout资源ID
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mLayoutResID
     *            所加载的layout资源ID
     * @author zhangqr
     */
    public void setmLayoutResID(int mLayoutResID) {
        this.mLayoutResID = mLayoutResID;
    }

    /**
     * 设置滚动框显示数据
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mDataList
     *            滚动框显示数据
     * @author zhangqr
     */
    public void setmDataList(ArrayList<Object> mDataList) {
        this.mDataList = mDataList;
    }
    
    /**
     * 设置按钮点击监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @param   mListener    the mListener to set
     * @author       zhangqr
     */
    public void setmListener(OnCommonRechargeClickListener mListener) {
        this.mListener = mListener;
    }
    
    /**
     * 设置滚动框滚动监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @param   mWheelScrollListener    the mWheelScrollListener to set
     * @author       zhangqr
     */
    public void setmWheelScrollListener(OnWheelScrollListener mWheelScrollListener) {
        this.mWheelScrollListener = mWheelScrollListener;
    }
    
    
    /**
     * 获得标题
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 标题
     * @author zhangqr
     */
    public String getmStrTitle() {
        return mStrTitle;
    }

    /**
     * 获得滚动框里面默认显示第几个
     * 
     * <br>
     * Created 2014-11-3 下午8:41:50
     * 
     * @return 滚动框里面默认显示第几个
     * @author zhangqr
     */
    public int getmIntDefaultItemIndex() {
        return mIntDefaultItemIndex;
    }

    /**
     * 获得剩余币种数量的提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 剩余币种数量的提示
     * @author zhangqr
     */
    public String getmStrRestTip() {
        return mStrRestTip;
    }

    /**
     * 获得兑换率提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 兑换率提示
     * @author zhangqr
     */
    public String getmStrRateTip() {
        return mStrRateTip;
    }

    /**
     * 获得提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 提示
     * @author zhangqr
     */
    public String getmStrTip() {
        return mStrTip;
    }

    /**
     * 获得左按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 左按钮文本
     * @author zhangqr
     */
    public String getmStrBtnLeft() {
        return mStrBtnLeft;
    }

    /**
     * 获得右按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 右按钮文本
     * @author zhangqr
     */
    public String getmStrBtnRight() {
        return mStrBtnRight;
    }
    
    /**
     * 获得所加载的layout资源ID
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 所加载的layout资源ID
     * @author zhangqr
     */
    public int getmLayoutResID() {
        return mLayoutResID;
    }
    
    /**
     * 获得滚动框显示数据
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 滚动框显示数据
     * @author zhangqr
     */
    public ArrayList<Object> getmDataList() {
        return mDataList;
    }
    
    /**
     * 获取按钮点击监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @return       mListener
     * @author       zhangqr
     */
    public OnCommonRechargeClickListener getmListener() {
        return mListener;
    }

    /**
     * 获取滚动框滚动监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @return       mWheelScrollListener
     * @author       zhangqr
     */
    public OnWheelScrollListener getmWheelScrollListener() {
        return mWheelScrollListener;
    }

    
    /**
     * isDismissWhenDone
     *
     * <br>Created 2014-11-4 下午8:32:28
     * @return       isDismissWhenDone
     * @author       zhangqr
     */
    public boolean isDismissWhenDone() {
        return isDismissWhenDone;
    }

    
    /**
     * setDismissWhenDone
     *
     * <br>Created 2014-11-4 下午8:32:28
     * @param   isDismissWhenDone    the isDismissWhenDone to set
     * @author       zhangqr
     */
    public void setDismissWhenDone(boolean isDismissWhenDone) {
        this.isDismissWhenDone = isDismissWhenDone;
    }

    
    /**
     * 是否显示滚动框
     *
     * <br>Created 2014-11-7 上午10:54:06
     * @return       mIsShowWheelView
     * @author       zhangqr
     */
    public boolean ismIsShowWheelView() {
        return mIsShowWheelView;
    }

    
    /**
     * 是否显示滚动框
     *
     * <br>Created 2014-11-7 上午10:54:06
     * @param   mIsShowWheelView    the mIsShowWheelView to set
     * @author       zhangqr
     */
    public void setmIsShowWheelView(boolean mIsShowWheelView) {
        this.mIsShowWheelView = mIsShowWheelView;
    }

    
    /**
     * getmStrTitleRest
     *
     * <br>Created 2014-11-10 下午2:44:14
     * @return       mStrTitleRest
     * @author       zhangqr
     */
    public String getmStrTitleRest() {
        return mStrTitleRest;
    }

    
    /**
     * setmStrTitleRest
     *
     * <br>Created 2014-11-10 下午2:44:14
     * @param   mStrTitleRest    the mStrTitleRest to set
     * @author       zhangqr
     */
    public void setmStrTitleRest(String mStrTitleRest) {
        this.mStrTitleRest = mStrTitleRest;
    }

    
    /**
     * getmStrTitleRate
     *
     * <br>Created 2014-11-10 下午2:44:14
     * @return       mStrTitleRate
     * @author       zhangqr
     */
    public String getmStrTitleRate() {
        return mStrTitleRate;
    }

    
    /**
     * setmStrTitleRate
     *
     * <br>Created 2014-11-10 下午2:44:14
     * @param   mStrTitleRate    the mStrTitleRate to set
     * @author       zhangqr
     */
    public void setmStrTitleRate(String mStrTitleRate) {
        this.mStrTitleRate = mStrTitleRate;
    }

}
